/**
 * Created by sunyxq on 16/3/26.
 */
angular.module('hyt.hzYunDanService',[])

    //获取承运单详情数据
    .factory('HzCydDetailFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz'),
            fhdDetailData = {},
            imgData = [];

        var resource = $resource(api);

        return {
            //获取承运单详情
            getDetail : function(id,sid,user_id,yh_id){
                return resource.save({
                    "ACTION_NAME" : "sqlKeyBiz.fetch#fetchFhdDetail",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        "fhd_id" : id
                    }
                }, function (r) {
                    if(r.ACTION_RETURN_CODE == "000000") {
                        fhdDetailData = r.ACTION_INFO;
                        $rootScope.$broadcast('FhdDetail.detailUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            getDetailContent : function(){
                if(fhdDetailData == undefined){
                    return false;
                }
                return fhdDetailData;
            },
            getImgData : function(id,sid,user_id,yh_id){
                return resource.save({
                    "ACTION_NAME" : "yanzhengBiz.downloadPIC#insert",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        "fhd_id" : id
                    }
                }, function (r) {
                    if(r.ACTION_RETURN_CODE == "000000") {
                        imgData = r.ACTION_INFO.imageList;
                        $rootScope.$broadcast('Image.detailUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            getImg : function(){
                if(imgData == undefined){
                    return false;
                }
                return imgData;
            }
        }
    })

    //获取承运单详情进程数据
    .factory('HzCydProcessFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz'),fhdProcessData = {},
            shdProcessData = {};

        var resource = $resource(api);

        return{
            //获取承运单进程
            getProcessfhd : function(id,sid,user_id,yh_id){
                return resource.save({
                    "ACTION_NAME" : "sqlKeyBiz.page#jincheng_query_huozhu",
                    "SID":sid,
                    "USER_ID":user_id,
                    "ACTION_INFO" : {
                        'yh_id':yh_id,
                        "fhd_id" : id,
                        'khlx':2
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000") {
                        console.log(r);
                        fhdProcessData = r.ACTION_INFO.RESULT_LIST;

                        $rootScope.$broadcast('FhdProcess.processUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },

            getfhd : function(){
                if(fhdProcessData == undefined){
                    return false;
                }
                return fhdProcessData;
            }
        }
    })

    //获取发货单列表数据
    .factory('HzCydListFactory',function($resource, $rootScope,ENV,Storage,$ionicLoading,$state){
        var APIUrl =  Storage.get('fwqdz'),
            fhdListData = {},
            shdListData = {},
            cydListData = {},
            result,
            pagesize = 10;

        var resource = $resource(APIUrl);

        return {
            //获取承运单列表数据
            getFhdList: function(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt) {
                var hasNextPage =true;    //判断是否有下一页

                return resource.save({
                    'ACTION_NAME': 'sqlKeyBiz.page#fhdList_fhr_query',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO': {
                        'yh_id':yh_id,
                        'lxdh':lxdh,
                        'cydh':cydh,
                        'kssj':kssj,
                        'jssj':jssj,
                        'shrdh':shrdh,
                        'shrmc':shrmc,
                        'fhdzt':fhdzt,
                        'minCyzt':minCyzt,
                        'maxCyzt':maxCyzt,
                        'pagesize':'10',
                        'current_page':'1'
                    }
                }, function(r) {
                    console.log(r);
                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        fhdListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };

                        $rootScope.$broadcast('FhdList.fhdUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            //获取承运单列表单页数据
            getFhd : function(){
                if(fhdListData[pagesize] == undefined){
                    return false;
                }
                return fhdListData[pagesize];
            },

            //获取承运单列表更多页数据
            getMoreFhd:function(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(fhdListData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = fhdListData[pagesize].hasNextPage;
                var current_page = fhdListData[pagesize].current_page;
                var moreFhdListData = fhdListData[pagesize].data;
                resource.save({
                    'ACTION_NAME': 'sqlKeyBiz.page#fhdList_fhr_query',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO': {
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'yh_id':yh_id,
                        'lxdh':lxdh,
                        'cydh':cydh,
                        'kssj':kssj,
                        'jssj':jssj,
                        'shrdh':shrdh,
                        'shrmc':shrmc,
                        'fhdzt':fhdzt,
                        'minCyzt':minCyzt,
                        'maxCyzt':maxCyzt
                    }
                }, function (r) {

                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        moreFhdListData = moreFhdListData.concat(r.ACTION_INFO.RESULT_LIST);

                        fhdListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreFhdListData
                        };

                        $rootScope.$broadcast('FhdList.fhdUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },

            hasNextPageFhd: function() {
                if (fhdListData[pagesize] === undefined) {
                    return false;
                }
                return fhdListData[pagesize].hasNextPage;
            },
            getShdList: function(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt) {
                var hasNextPage =true;    //判断是否有下一页

                return resource.save({
                    'ACTION_NAME': 'sqlKeyBiz.page#fhdList_shr_query',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO': {
                        'yh_id':yh_id,
                        'pagesize':'10',
                        'current_page':'1',
                        'lxdh':lxdh,
                        'djsj_ks':kssj,
                        'djsj_js':jssj,
                        'cydh':cydh,
                        'shrdh':shrdh,
                        'shrmc':shrmc,
                        'fhdzt':fhdzt,
                        'minCyzt':minCyzt,
                        'maxCyzt':maxCyzt
                    }
                }, function(r) {
                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        shdListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };

                        $rootScope.$broadcast('ShdList.shdUpdated');
                    }
                });
            },
            //获取承运单列表单页数据
            getShd : function(){
                if(shdListData[pagesize] == undefined){
                    return false;
                }
                return shdListData[pagesize];
            },

            //获取承运单列表更多页数据
            getMoreShd:function(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(shdListData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = shdListData[pagesize].hasNextPage;
                var current_page = shdListData[pagesize].current_page;
                var moreShdListData = shdListData[pagesize].data;

                //console.log(moreCydListData);

                resource.save({
                    'ACTION_NAME': 'sqlKeyBiz.page#query_shr_fhd',
                    'SID':sid,
                    'USER_ID':user_id,
                    'ACTION_INFO': {
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'yh_id':yh_id,
                        'lxdh':lxdh,
                        'djsj_ks':kssj,
                        'djsj_js':jssj,
                        'cydh':cydh,
                        'shrdh':shrdh,
                        'shrmc':shrmc,
                        'fhdzt':fhdzt,
                        'minCyzt':minCyzt,
                        'maxCyzt':maxCyzt
                    }
                }, function (r) {

                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }

                        moreShdListData = moreShdListData.concat(r.ACTION_INFO.RESULT_LIST);

                        shdListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreShdListData
                        };

                        $rootScope.$broadcast('ShdList.shdUpdated');
                    }
                })
            },

            hasNextPageShd: function() {
                if (shdListData[pagesize] === undefined) {
                    return false;
                }
                return shdListData[pagesize].hasNextPage;
            },
            getDeleteFhd:function(id,sid,user_id,yh_id){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'beanBiz.deleteFhd#U_fhd',
                    ACTION_INFO : {
                        'fhd_id':id,
                        'yh_id':yh_id
                    }
                },function(r){
                    console.log(r);
                    if(r.ACTION_RETURN_CODE == "000000"){
                        result= r.ACTION_INFO.result;
                    } else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                    $rootScope.$broadcast('Delete.fhdUpdated');
                });
            },
            getResult : function(){
                return result;
            }
        };
    })
    //修改发货单
    .factory('changeFhdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz');
        var resource = $resource(api);
        var result;
        return{
            getChangeFhd:function(id,sid,user_id,yh_id,fhd,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,fhdzt,fhdlx){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.transUpdate#updateFhd',
                    ACTION_INFO : {
                        'fhd_id':id,
                        'yh_id':yh_id,
                        'fhdzt':fhdzt,
                        'fhdlx':fhdlx,
                        'shfs':shfs,
                        'fhrmc':fhd.fhrmc,
                        'fhrdh':fhd.fhrdh,
                        'fpro':fpro,
                        'fcity':fcity,
                        'farea':farea,
                        'spro':spro,
                        'scity':scity,
                        'sarea':sarea,
                        'fprocode':fproCode,
                        'fcitycode':fcityCode,
                        'fareacode':fareaCode,
                        'sprocode':sproCode,
                        'scitycode':scityCode,
                        'sareacode':sareaCode,
                        'fhrdz':fhd.fhrdz,
                        'shrmc':fhd.shrmc,
                        'shrdh':fhd.shrdh,
                        'shrdz':fhd.shrdz,
                        'hwmc':fhd.hwmc,
                        'fhrq':fhd.fhrq,
                        'zl':fhd.zl,
                        'sl':fhd.sl,
                        'tj':fhd.tj,
                        'dshk':fhd.dshk,
                        'bjsm':fhd.bjsm,
                        'tbzt':fhd.tbzt,
                        'sfhd':fhd.sfhd,
                        'bz':fhd.bz
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000"){
                        result= r.ACTION_INFO.result;
                    } else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                    $rootScope.$broadcast('Change.fhdUpdated');
                });
            },
            getResult : function(){
                return result;
            }
        }
    })
    //修改补单
    .factory('changeBdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
        var api =  Storage.get('fwqdz');
        var resource = $resource(api);
        var result;
        return{
            getChangeBd:function(id,sid,user_id,yh_id,buDan,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME :  'sqlKeyBiz.transUpdate#updateFhd',
                    ACTION_INFO : {
                        'fhd_id':id,
                        'yh_id':yh_id,
                        'cydh':buDan.cydh,
                        'dwmc':buDan.dwmc,
                        'shfs':shfs,
                        'fhdlx':3,
                        'fhdzt':3,
                        'fhrmc':buDan.fhrmc,
                        'fhrdh':buDan.fhrdh,
                        'fpro':fpro,
                        'fcity':fcity,
                        'farea':farea,
                        'spro':spro,
                        'scity':scity,
                        'sarea':sarea,
                        'fprocode':fproCode,
                        'fcitycode':fcityCode,
                        'fareacode':fareaCode,
                        'sprocode':sproCode,
                        'scitycode':scityCode,
                        'sareacode':sareaCode,
                        'fhrdz':buDan.fhrdz,
                        'shrmc':buDan.shrmc,
                        'shrdh':buDan.shrdh,
                        'shrdz':buDan.shrdz,
                        'hwmc':buDan.hwmc,
                        'cyrq':buDan.cyrq,
                        'zl':buDan.zl,
                        'sl':buDan.sl,
                        'tj':buDan.tj,
                        'bzlx':buDan.bzlx,
                        'yf_xf':buDan.yf_xf,
                        'yf_tf':buDan.yf_tf,
                        'yf_hf':buDan.yf_hf,
                        'yf_yj':buDan.yf_yj,
                        'yf_ycdk':buDan.yf_ycdk,
                        'xxf_xf':buDan.xxf_xf,
                        'xxf_qf':buDan.xxf_qf,
                        'shf':buDan.shf,
                        'thf':buDan.thf,
                        'dshk':buDan.dshk,
                        'bjsm':buDan.bjsm,
                        'tbzt':buDan.tbzt,
                        'sfhd':buDan.sfhd,
                        'bf':buDan.bf,
                        'yf_df':buDan.yf_df,
                        'dsk_df':buDan.dsk_df,
                        'bz':buDan.bz
                    }
                },function(r){
                    if(r.ACTION_RETURN_CODE == "000000"){
                        result= r.ACTION_INFO.result;
                    } else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                    $rootScope.$broadcast('Change.bdUpdated');
                });
            },
            getResult : function(){
                return result;
            }
        }
    })
    //签收
    .factory('receiptFactory',function($resource,$rootScope,ENV,Storage){
        var api =  Storage.get('fwqdz');
        var resource = $resource(api);
        var qsResult;
        return{
            qianshou:function(sid,user_id,yh_id,cyd_id,lxdh){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME:'sqlKeyBiz.transUpdate#yonghuqianshou_set',
                    ACTION_INFO:{
                        'cyd_id':cyd_id,
                        'qsrdh':lxdh,
                        'yh_id':yh_id
                    }
                },function(resp){
                    qsResult=resp.ACTION_RETURN_CODE;
                    $rootScope.$broadcast('Hz_qsUpdated');
                })
            },
            getQsResult:function(){
                return qsResult;
            }
        }
    });
